package cn.nfwx.tv.controller.me;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import cn.nfwx.tv.adapter.TvHistroyAdapter;
import cn.nfwx.tv.base.BaseFragment;
import cn.nfwx.tv.callback.ResponseListener;
import cn.nfwx.tv.events.PlayEvents;
import cn.nfwx.tv.fragment.me.MineAllFragment;
import cn.nfwx.tv.fragment.me.MineFragment;
import cn.nfwx.tv.helper.DialogFragmentHelper;
import cn.nfwx.tv.helper.ThreadHelper;
import cn.nfwx.tv.manager.WatchRecordManager;
import cn.nfwx.tv.model.Result;
import cn.nfwx.tv.model.TvHistroy;
import cn.nfwx.tv.util.ToastUtil;
import cn.nfwx.tv.widget.LoadView;

public class MineTvHistroyController implements TvHistroyAdapter.OnCheckedChangeDelListener {
    private Context mContext;
    private MineAllFragment mFragment;
    private RecyclerView mRecyclerView;
    private LoadView mLoadView;
    private TwinklingRefreshLayout trl_mine;
    private View notDataView;
    private TextView edit_mine_btn;
    private TextView tv_mine_select;

    private List<TvHistroy> tvHistroys = new ArrayList<>();

    private TvHistroyAdapter tvHistroyAdapter;

    private String requestViewHistroyCommand, requestViewHistroyDelCommand;


    public MineTvHistroyController(Context mContext, MineAllFragment fragment) {
        this.mContext = mContext;
        mFragment = fragment;
    }

    public void init(RecyclerView recyclerView, LoadView loadView, final TwinklingRefreshLayout trl_mine, View notDataView, TextView edit_mine_btn) {
        mRecyclerView = recyclerView;
        mLoadView = loadView;
        this.trl_mine = trl_mine;
        this.notDataView = notDataView;
        this.edit_mine_btn = edit_mine_btn;
        edit_mine_btn.setVisibility(View.GONE);

        if (tvHistroyAdapter == null) {
            tvHistroyAdapter = new TvHistroyAdapter();
        }
        mRecyclerView.setAdapter(tvHistroyAdapter);
        tvHistroyAdapter.setOnCheckedChangeDelListener(this);

        tvHistroyAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Object o = adapter.getData().get(position);
                if (o instanceof TvHistroy) {
                    TvHistroy tvHistroy = (TvHistroy) o;
                    if (tvHistroyAdapter.isDelete()) {
                        tvHistroy.setCheck(!tvHistroy.isCheck());
                        adapter.notifyItemChanged(position);
                    } else {
                        PlayEvents events = new PlayEvents(tvHistroy.getProgram_id(), tvHistroy.getUrl(), null, tvHistroy.getProgram_name(), tvHistroy.getPosition());
                        if (mFragment.getParentFragment() instanceof MineFragment) {
                            MineFragment mineFragment = (MineFragment) mFragment.getParentFragment();
                            mineFragment.isBackEvent = false;
                            events.setFragment(PlayEvents.MINE);
                            events.setMePosition(MineFragment.POSITION_HISTORY);
                            events.setTag("mineFragment");
                        }
                        EventBus.getDefault().post(events);
                    }
                }
            }
        });
    }

    public void bindTvHistroyData() {
        tvHistroyAdapter.setEmptyView(new View(mContext));
        tvHistroyAdapter.setNewData(new ArrayList<TvHistroy>());
        if (tvHistroys.isEmpty()) {
            //mLoadView.startLoading(trl_mine);
            requestViewHistroyCommand = WatchRecordManager.requestViewHistroyList(mContext, responseListener);
        } else {
            tvHistroyAdapter.setNewData(tvHistroys);
            trl_mine.finishRefreshing();
        }
    }

    ResponseListener<String> responseListener = new ResponseListener<String>() {
        @Override
        public void onResponse(final String requestTag, final String response) {
            ThreadHelper.post(new Runnable() {
                @Override
                public void run() {
                    if (requestTag.equals(requestViewHistroyCommand)) {
                        trl_mine.finishRefreshing();
                        mLoadView.removeParentView(trl_mine);
                        Result<List<TvHistroy>> result = JSON.parseObject(response, new TypeReference<Result<List<TvHistroy>>>() {
                        }.getType());
                        if (Result.RESULT_SUCCESS.equals(result.getMessage())) {
                            if (result.getData().isEmpty()) {
                                edit_mine_btn.setVisibility(View.GONE);
                                mFragment.hideBottom();
                                tvHistroyAdapter.setNewData(result.getData());
                                tvHistroyAdapter.setEmptyView(notDataView);
                            } else {
                                edit_mine_btn.setVisibility(View.VISIBLE);
                                tvHistroys.clear();
                                tvHistroys.addAll(result.getData());
                                tvHistroyAdapter.setNewData(result.getData());
                            }
                        }
                    } else if (requestTag.equals(requestViewHistroyDelCommand)) {
                        DialogFragmentHelper.closeLoadingDialog(mFragment.getChildFragmentManager());
                        Result result = JSON.parseObject(response, new TypeReference<Result>() {
                        }.getType());
                        if (result.getMessage().equals(Result.RESULT_SUCCESS)) {
                            tvHistroys.clear();
                            requestViewHistroyCommand = WatchRecordManager.requestViewHistroyList(mContext, responseListener);
                        }
                    }
                }
            });
        }

        @Override
        public void onError(final String requestTag, Throwable t) {
            ThreadHelper.post(new Runnable() {
                @Override
                public void run() {
                    if (requestTag.equals(requestViewHistroyCommand)) {
                        mLoadView.loadError(trl_mine);
                        trl_mine.finishRefreshing();
                    } else if (requestTag.equals(requestViewHistroyDelCommand)) {
                        DialogFragmentHelper.closeLoadingDialog(mFragment.getChildFragmentManager());
                        ToastUtil.show(mContext, "删除失败");
                    }
                }
            });
        }

        @Override
        public void onFinish() {

        }
    };

    public boolean isDelete() {
        return tvHistroyAdapter.isDelete();
    }

    public void changeDelete() {
        tvHistroyAdapter.setDelete(!tvHistroyAdapter.isDelete());
    }

    public boolean isSelectAll() {
        return tvHistroyAdapter.isSelectAll();
    }

    public void changeSelectAll() {
        tvHistroyAdapter.setSelectAll(!isSelectAll());
    }

    public void deleteSelect() {
        List<TvHistroy> tvHistroys = tvHistroyAdapter.getDeleteDatas();
        List<Integer> programids = new ArrayList<>();
        for (TvHistroy tvHistroy : tvHistroys) {
            if (!programids.contains(tvHistroy.getProgram_id())) {
                programids.add(tvHistroy.getProgram_id());
            }
        }
        if (!programids.isEmpty()) {
            DialogFragmentHelper.showLoadingDialog(mFragment.getChildFragmentManager());
            requestViewHistroyDelCommand = WatchRecordManager.requestDelViewHistroy(mContext, programids, responseListener);
        } else {
            ToastUtil.show(mContext, "选择要删除的节目");
        }
    }

    @Override
    public void onCheckedAllChanged(boolean stats) {
        if (stats) {
            tv_mine_select.setText("取消全选");
        } else {
            tv_mine_select.setText("全选");
        }
    }

    public void setDeteleView(TextView deteleView) {
        this.tv_mine_select = deteleView;
    }
}
